#include "graph.h"
#include <bits/stdc++.h>

using namespace std;
/*
#define MAX_N 201
#define N_MOVE_MAX 1500000

static int N;
static int M;
static int X;
static int R;
static int n_roads[MAX_N];
static int road[MAX_N][MAX_N];
static int road_idx[MAX_N][MAX_N];

static int color[MAX_N];
static int pos;
static int last_road;

static int called_answer[MAX_N];
static int call_move;

static int expected_answer[MAX_N];

static void Wrong(int message) {
    printf("Wrong Answer [%d]\n", message);
    exit(1);
}

void Answer(int D, int A) {
    if (!(1 <= D && D <= R)) {
        Wrong(1);
    }
    if (called_answer[D] != -1) {
        Wrong(2);
    }

    called_answer[D] = A;
}

void Move(int I, int V) {
    if (I < 1 || n_roads[pos] < I) {
        Wrong(5);
    }
    if (V < 1 || X < V) {
        Wrong(6);
    }

    ++call_move;

    if (call_move > N_MOVE_MAX) {
        Wrong(7);
    }
    --I;
    color[pos] = V;
    last_road = road_idx[road[pos][I]][pos] + 1;
    pos = road[pos][I];
}

int NumberOfRoads() {
    return n_roads[pos];
}

int LastRoad() {
    return last_road;
}

int Color() {
    return color[pos];
}
*/
namespace ZYH {
   const int kMaxN = 205;

   bool G[kMaxN][kMaxN];

   int tim;

   bool fzb[kMaxN][kMaxN], sb[kMaxN][kMaxN];
   int fzb_id[kMaxN][kMaxN];

   void dfs1(int fa) {
      int x = ++tim;
      //cout << "into1 : " << x << ' ' << fa << '\n';
      if (fa) G[x][fa] = G[fa][x] = 1;
      int to_fa_edge = (x == 1) ? 0 : LastRoad();
      int d = NumberOfRoads();
      for (int i = 1; i <= d; ++i) {
         if (i == to_fa_edge) continue;
         Move(i, 1);
         if (Color() == 3) fzb[x][i] = 1;
         Move(LastRoad(), Color());
      }
      for (int i = 1; i <= d; ++i) {
         if (i == to_fa_edge) continue;
         if (fzb[x][i]) continue;
         Move(i, 3);
         if (Color() == 3) Move(LastRoad(), Color());
         else {
            sb[x][i] = 1;
            //cout << "sb : " << x << ' ' << i << '\n';
            dfs1(x);
         }
      }
      if (to_fa_edge) Move(to_fa_edge, 3);
   }

   int getPow(int x, int y) {
      int res = 1;
      for (int i = 1; i <= y; ++i) res = res * x;
      return res;
   }

   void dfs2(int p) {
      int x = ++tim;
      //cerr << "into2 : " << x << '\n';
      int d = NumberOfRoads();
      int to_fa_edge = (x == 1) ? 0 : LastRoad();
      if (x >= getPow(3, p)) {
         for (int i = 1; i <= d; ++i) {
            if (fzb[x][i]) {
               Move(i, Color());
               fzb_id[x][i] += (Color() - 1) * getPow(3, p);
               Move(LastRoad(), Color());
            }
         }
      }
      for (int i = 1; i <= d; ++i) {
         if (!sb[x][i]) continue;
         //cerr << x / getPow(3, p) % 3 + 1 << '\n';
         Move(i, x / getPow(3, p) % 3 + 1);
         dfs2(p);
      }
      if (to_fa_edge) Move(to_fa_edge, Color());
   }

   int n;
   int dis[kMaxN][kMaxN];
   int cnt[kMaxN];

   void getDis() {
      n = tim;
      memset(dis, 0x3f, sizeof(dis));
      for (int i = 1; i <= n; ++i) {
         dis[i][i] = 0;
      }
      for (int i = 1; i <= n; ++i) {
         for (int j = 1; j <= n; ++j) {
            if (fzb[i][j]) G[i][fzb_id[i][j]] = G[fzb_id[i][j]][i] = 1;
         }
      }
      for (int i = 1; i <= n; ++i) {
         for (int j = 1; j <= n; ++j) {
            if (G[i][j]) dis[i][j] = min(dis[i][j], 1);
         }
      }
      for (int k = 1; k <= n; ++k) {
         for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= n; ++j) {
               dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
            }
         }
      }
      //cout << "n = " << n << '\n';
      for (int i = 1; i <= n; ++i) {
         for (int j = i + 1; j <= n; ++j) {
            //if (G[i][j]) cerr << "edge : " << i << ' ' << j << '\n';
            ++cnt[dis[i][j]];
         }
      }
   }
}
void Inspect(int R) {
   ZYH::tim = 0;
   ZYH::dfs1(0);
   //cout << "move = " << call_move << '\n';
   for (int i = 0; i <= 4; ++i) {
      ZYH::tim = 0;
      ZYH::dfs2(i);
   }
   ZYH::getDis();
   for (int i = 1; i <= R; ++i) Answer(i, ZYH::cnt[i]);
}
/*
int main(void) {
    int i, j;
    if (scanf("%d%d%d", &N, &X, &R) != 3) {
        fprintf(stderr, "error: cannot read N, X and R.\n");
        exit(1);
    }

    if (N < 2 || MAX_N < N) {
        fprintf(stderr, "error: N is out of bounds.\n");
        exit(1);
    }
   int M = 0;
    for (i = 0; i < N; ++i) {
        scanf("%d", &(n_roads[i]));
         M += n_roads[i];
        for (j = 0; j < N; ++j)
            road_idx[i][j] = -1;
        for (j = 0; j < n_roads[i]; ++j) {
            scanf("%d", &(road[i][j]));
            --road[i][j];
            road_idx[i][road[i][j]] = j;
        }
    }

    for (i = 1; i <= R; ++i) {
        scanf("%d", &(expected_answer[i]));
    }

    for (i = 0; i < N; ++i) {
        color[i] = 1;
    }

    pos = 0;
    last_road = -1;
    call_move = 0;
    for (i = 1; i <= R; ++i)
        called_answer[i] = -1;

    Inspect(R);

    for (i = 1; i <= R; ++i) {
        if (called_answer[i] == -1) {
            Wrong(3);
        }
        if (called_answer[i] != expected_answer[i]) {
            Wrong(4);
        }
    }

    printf("Accepted: #move = %d\n", call_move);
      cout << "M = " << M / 2 << '\n';
    return 0;
}
*/
